package com.service.lqj;

import com.dao.BaseDao;
import com.entity.Knowledge;
import com.entity.PageManager;
import com.entity.Varietiess;

import java.io.File;
import java.util.List;

public class KnowledgeService extends BaseDao {

    /*public Object[] getObjArr(User user){
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
        user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
        user.getPic()};
        return params;
    }*/

    /**
     * 增加用户数据
     * @param knowledges
     * @return
     */
    public boolean addKnowledge(Knowledge knowledges) {
        /*String sql = "INSERT INTO `rz1217`.`user`(`username`, `password`, " +
                "`name`, `sex`, `age`, `birthday`, `phone`, " +
                "`height`, `pic`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] params = getObjArr(user);*/
        try {
            return insert(knowledges) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 分页查询
     * @param pm
     * @return
     */
    public List<Knowledge> queryKnowledge(PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from knowledge limit ?,?";
        return findList(Knowledge.class, sql, start, end);
    }

    public List<Knowledge> queryKnowledges() {
        String sql = "select * from knowledge";
        return findList(Knowledge.class, sql);
    }

    public Knowledge queryKnowledge(String pm) {

        try {
            return (Knowledge) queryKnowledge(new Knowledge(pm));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 分页查询
     * @return
     */
//    public List<Knowlede> queryVarieties() {
//        String sql = "select * from Varieties";
//        return findList(Knowlede.class, sql);
//    }


//    public int queryVarietiesTotalCount() {
//        String sql = "select count(*) from Varietiess";
//        return count(sql);
//    }


    public int queryknowledgeTotalCount() {
        String sql = "select count(*) from Knowledge";
        return count(sql);
    }

    public boolean delKnowledge(String knowledge) {
        boolean result = false;
        try {
            result = deleteKnowledge(new Knowledge(knowledge))> 0;
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public int deleteKnowledge(Object obj) throws Exception {
        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "delete from "+tableName +" where title = ?";

        return update(sql, clas.getDeclaredMethod("getTitle").invoke(obj));
    }

    public boolean updateKnowledge(Knowledge knowledge) {
        Knowledge knowledge1 = queryKnowledge(knowledge.getTitle());
        try {
            boolean result = updateKnowledges(knowledge) > 0;
            //修改成功，并且传递的图片和数据库的图片不同时，才删除图片文件
            if (result && knowledge.getPic() != null
                    && !knowledge.getPic().equals(knowledge1.getPic())) {
                //删除原图片
                new File(knowledge1.getPic()).delete();
            }
            if (result && knowledge.getPicture() != null
                    && !knowledge.getPicture().equals(knowledge1.getPicture())) {
                //删除原图片
                new File(knowledge1.getPicture()).delete();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

//    public boolean delKnowledge(String varieties) {
//        Knowledge knowledge = queryKnowledge(varieties);
//        boolean result = false;
//        try {
//            result = deleteknowledge(new Varietiess(varieties))> 0;
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    private boolean deleteknowledge(Varietiess varietiess) {
//    }
}